#include "WavFile.h"

bool WavFile::CreateFrom(const char* path)
{
    std::ifstream file(path, std::ios::binary);
    if (!file.is_open()) {
        
        LOGI("cannot open file %s", path);
        return false;
    }

    file.read(reinterpret_cast<char*>(&header), sizeof(WAVHeader));

    if (std::string(header.chunkID, 4) != "RIFF" || std::string(header.format, 4) != "WAVE") {
        LOGE("is not a avail wav file ");
        return false;
    }


    data = new unsigned char[header.subchunk2Size];
    file.read((char*)data, header.subchunk2Size);

    file.close();

    return true;
}

uint32_t WavFile::DataSize()
{
    return this->header.subchunk2Size;
}

uint8_t* WavFile::Data()
{
    return data;
}

uint32_t WavFile::SampleRate()
{
    return header.sampleRate;
}

uint16_t WavFile::Format()
{
    return AL_FORMAT_STEREO16;
}

void WavFile::Free()
{
    delete[] this->data;
}
